<?php

/**
* @current request.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2016年12月8日 下午5:10:28
*/

class Request{
    
	private static $requestPath = null;
	private static $requestUri = null;
	private static $header = null;
	private static $serverInfo = null;
	
	public static function path(){
		if (!self::$requestPath){
			$path = $_SERVER['REQUEST_URI'];
			$pos = strripos($path,'?');
			if ($pos !== false)
			{
				$path = substr($path,0,$pos);
			}
			self::$requestPath = $path;
		}
		return self::$requestPath;
	}
	
	public static function method(){
		return $_SERVER['REQUEST_METHOD'];
	}
	
	public static function isPOST(){
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			return true;
		}
		return false;
	}

	public static function isGET(){
		if ($_SERVER['REQUEST_METHOD'] == 'GET'){
			return true;
		}
		return false;
	}
	
	public static function url(){
		if (!self::$requestUri){
			self::$requestUri = urldecode($_SERVER['REQUEST_URI']);
		}
		return self::$requestUri;
	}
	
	public static function header($infoType){
		$result = null;
		$arr = self::headers();
		if (isset($arr[$infoType])){
			$result = $arr[$infoType];
		}
		return $result;
	}

	public static function headers(){
		if (!self::$header){
			$headers = array();
			foreach($_SERVER as $key => $value){
				if(substr($key, 0, 5) === 'HTTP_'){
					$key = substr($key, 5);
					$key = strtolower($key);
					$key = str_replace('_', ' ', $key);
					$key = ucwords($key);
					$key = str_replace(' ', '-', $key);
					$headers[$key] = $value;
				}
			}
			self::$header = $headers;
		}
		return self::$header;
	}
	
	public static function server($infoType){
		$result = null;
		$arr = self::serverInfo();
		if (isset($arr[$infoType])){
			$result = $arr[$infoType];
		}
		return $result;
	}
	
	public static function serverInfo(){
		if (!self::$serverInfo){
			$serverInfo = array();
			foreach($_SERVER as $key => $value){
				if(substr($key, 0, 7) === 'SERVER_'){
					$key = substr($key, 7);
					$key = strtolower($key);
					$key = str_replace('_', ' ', $key);
					$key = ucwords($key);
					$key = str_replace(' ', '-', $key);
					$serverInfo[$key] = $value;
				}
			}
			self::$serverInfo = $serverInfo;
		}
		return self::$serverInfo;
	}
	
	public static function get($key,$default=null){
		return self::getParam($_GET, $key, $default);
	}
	
	public static function post($key,$default=null){
		return self::getParam($_POST, $key, $default);
	}
	
	public static function all($key,$default=null){
		return self::get($key,$default) || self::post($key,$default);
	}
	
	public static function file($name){
		if (isset($_FILES[$name])){
			return $_FILES[$name];
		}
	}
	
	public static function allFiles(){
		return $_FILES;
	}
	
	public static function cookie($key){
		if(isset($_COOKIE[$key])){
			return $_COOKIE[$key];
		}
	}
	
	public static function allCookies(){
		return $_COOKIE;
	}
	
	private static function getParam($arr,$key,$default){
		$val = null;
		if (isset($arr[$key])){
			if (is_array($arr[$key])){
				$val = $arr[$key];
				if (!$val && $default){
					$val = $default;
				}
			}
			else{
				$val = trim($arr[$key]);
			}
		}
		if ($val == '' || $val == null){
			if ($default != null){
				$val = $default;
			}
		}
		return $val;
	}
}
